<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button v-focus="'import_employee'" type="warning" size="small" @click="$router.push('/employees/import')">excel导入</el-button>
          <el-button v-focus="'export_employee'" type="danger" size="small" @click="hExport">excel导出</el-button>
          <el-button v-focus="'tianjia'" type="primary" size="small" @click="dialogVisible=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="employeesList" :destroy-on-close="true">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template slot-scope="scope">
              <image-holder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id='+scope.row.id)">查看</el-button>
              <el-button type="text" size="small" @click="hAssignRole(scope.row)">分配角色</el-button>
              <el-button type="text" size="small" @click="deleteUser(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page.page"
            :page-sizes="[1, 3, 4, 5]"
            :pager-count="5"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        title="新增员工"
        :visible.sync="dialogVisible"
        width="60%"
      >
        <empDialog v-if="dialogVisible" @close="dialogVisible=false" @update-empolyee="hUpdateEmpolyee" />
      </el-dialog>

      <el-dialog
        title="分配角色"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogRole"
      >
        <assign-role v-if="showDialogRole" :employee-id="curEmployeId" @close="showDialogRole=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
// 引入组件
import AssignRole from './assignRole.vue'
import empDialog from './empDialog.vue'
import { getEmployeeslist } from '@/api/employees'
import { delEmployee } from '@/api/employees'
export default {
  components: { empDialog, AssignRole },
  data() {
    return {
      employeesList: [],
      page: {
        page: 1,
        size: 5
      }, // 分页
      total: 0, // 总页码
      dialogVisible: false,
      showDialogRole: false,
      curEmployeId: '' // 当前的员工编号
    }
  },
  created() {
    console.log(this.$router)
    this.loadEmployeeslist()
  },
  methods: {
    async loadEmployeeslist() {
      const res = await getEmployeeslist(this.page)
      this.employeesList = res.data.rows
      this.total = res.data.total
      console.log(res)
    }, // 获取员工列表数据
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.size = val
      this.loadEmployeeslist()
    }, // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.page = val
      this.loadEmployeeslist()
    }, // 分页
    // 收到子组件抛出的添加成功的事件
    hUpdateEmpolyee() {
      this.dialogVisible = false
      this.loadEmployeeslist()
    },
    async deleteUser(id) {
      console.log(id)
      const res = await delEmployee(id)
      if (res.success) {
        // 如果删除第最后一页的最一条数据之后，页面会显示不正常
        if (this.employeesList.length === 1) {
          this.page.page--
          if (this.page.page <= 0) {
            this.page.page = 1
          }
        }
        this.$message({
          message: res.message,
          type: 'success'
        })
      } else {
        this.$message.error(res.data.message)
      }
      this.loadEmployeeslist()
    },
    formatConversion(rows) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const enKeys = Object.keys(rows[0])
      const header = enKeys.map(item => {
        return map[item]
      })
      const data = rows.map(item => {
        return Object.values(item)
      })
      return {
        header,
        data
      }
    },
    async hExport() {
      const res = await getEmployeeslist(this.page)
      const { header, data } = this.formatConversion(res.data.rows)
      import('@/components/vendor/Export2Excel').then(excel => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    hAssignRole({ id }) {
      console.log('当前要分配角色id是', id)
      this.showDialogRole = true
      this.curEmployeId = id
    }
  }
}
</script>
